using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface.Presentation.Input;
using System.Threading;
using System.Diagnostics;

namespace Master
{
    /// <summary>
    /// Interaction logic for SurfaceWindow1.xaml
    /// </summary>
    public partial class SurfaceWindow1 : SurfaceWindow
    {
        public static SurfaceWindow1 INSTANCE;
        private static Boolean initialized = false;
        /// <summary>
        /// Default constructor.
        /// </summary>
        public SurfaceWindow1()
        {
            if (SurfaceWindow1.initialized)
                throw new InvalidOperationException();
            INSTANCE = this;
            SurfaceWindow1.initialized = true;

            InitializeComponent();

            // Add handlers for window availability events
            AddWindowAvailabilityHandlers();
            hookUpEvents();
        }

        public void hookUpEvents()
        {
            CarManager.getInstance().hookupSurfaceWindowEvent();
            foreach (UIElement child in Map.INSTANCE.Children) if (child is InterSection)
            {
                this.Closed += ((InterSection)child).stopStrategy;
                this.Loaded += ((InterSection)child).init;
            }
        }

        /// <summary>
        /// Occurs when the window is about to close. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            // Remove handlers for window availability events
            RemoveWindowAvailabilityHandlers();
        }

        /// <summary>
        /// Adds handlers for window availability events.
        /// </summary>
        private void AddWindowAvailabilityHandlers()
        {
            // Subscribe to surface window availability events
            ApplicationServices.WindowInteractive += OnWindowInteractive;
            ApplicationServices.WindowNoninteractive += OnWindowNoninteractive;
            ApplicationServices.WindowUnavailable += OnWindowUnavailable;
        }

        /// <summary>
        /// Removes handlers for window availability events.
        /// </summary>
        private void RemoveWindowAvailabilityHandlers()
        {
            // Unsubscribe from surface window availability events
            ApplicationServices.WindowInteractive -= OnWindowInteractive;
            ApplicationServices.WindowNoninteractive -= OnWindowNoninteractive;
            ApplicationServices.WindowUnavailable -= OnWindowUnavailable;
        }

        /// <summary>
        /// This is called when the user can interact with the application's window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnWindowInteractive(object sender, EventArgs e)
        {
            //TODO: enable audio, animations here
        }

        /// <summary>
        /// This is called when the user can see but not interact with the application's window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnWindowNoninteractive(object sender, EventArgs e)
        {
            //TODO: Disable audio here if it is enabled

            //TODO: optionally enable animations here
        }

        /// <summary>
        /// This is called when the application's window is not visible or interactive.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnWindowUnavailable(object sender, EventArgs e)
        {
            //TODO: disable audio, animations here
        }

        //private void btnAddCarTop_Click(object sender, RoutedEventArgs e)
        //{
        //    int NumberOfCars = (int)surfaceSliderTop.Value;
        //    map.addCar(spawnPointTop, NumberOfCars);
        //}

        //private void btnAddCarRight_Click(object sender, RoutedEventArgs e)
        //{
        //    int NumberOfCars = (int)surfaceSliderRight.Value;
        //    map.addCar(spawnPointRight, NumberOfCars);
        //}

        //private void btnAddCarBottom_Click(object sender, RoutedEventArgs e)
        //{
        //    int NumberOfCars = (int)surfaceSliderBottom.Value;
        //    map.addCar(spawnPointBottom, NumberOfCars);
        //}

        //private void btnAddCarLeft_Click(object sender, RoutedEventArgs e)
        //{
        //    int NumberOfCars = (int)surfaceSliderLeft.Value;
        //    map.addCar(spawnPointLeft, NumberOfCars);
        //}

        //private void surfaceSliderTop_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        //{
        //    int value = (int)e.NewValue;
        //    lblNumberOfCarsTop.Content = value;
        //}

        //private void surfaceSliderRight_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        //{
        //    int value = (int)e.NewValue;
        //    lblNumberOfCarsRight.Content = value;
        //}

        //private void surfaceSliderBottom_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        //{
        //    int value = (int)e.NewValue;
        //    lblNumberOfCarsBottom.Content = value;
        //}

        //private void surfaceSliderLeft_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        //{
        //    int value = (int)e.NewValue;
        //    lblNumberOfCarsLeft.Content = value;
        //}
    }
}